//
//  TimelineCellTopView.swift
//  01sinablog
//
//  Created by ZhangZiang on 15/12/18.
//  Copyright © 2015年 ZhangZiang. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class TimelineCellTopView: UIView {

    //当没有配图时候,底部约束应该相对于内容label
    var bottomConstraint : Constraint?
    
    @objc private func sourceBtnDidClicked() {
//        let nav = BaseNavigationController()
        let webView = WebViewController()
        
//        nav.addChildViewController(webView)
        webView.urlStr = timeline?.source?.sourceURLAndText().urlStr
        self.navController()?.pushViewController(webView, animated: true)
    }
    
    var timeline : Timeline? {
        didSet{
            iconView.sd_setImageWithURL(timeline?.user?.headURL, placeholderImage: UIImage(named: "avatar_default_big"))
            nameView.text = timeline?.user?.name
            mbrank.image = timeline?.user?.mbrankImage
            verified_type_image.image = timeline?.user?.verified_type_image
//            textView.text = timeline?.text
            
            //为配图赋值数据
            pictureView.imageURLs = timeline?.imageURLs
            
            //TODO: time & source
//            print(timeline?.created_at)
            let date = NSDate().sinaDate(timeline?.created_at ?? "")
            timeView.text = date?.fullStr()
            
//            print(timeline?.source)
        sourceView.setTitle(timeline?.source?.sourceURLAndText().text, forState: .Normal)
            
            //给富文本
            if let text = timeline?.text {
                textView.attributedText =
            EmoticonManager.sharedEmoticonManager.textStringToEmoticonImageString(text, font: textView.font)
            }
            //根据是否有配图来更新约束
            self.bottomConstraint?.uninstall()
            
            if let urls = timeline?.imageURLs where urls.count > 0
            {
                self.snp_updateConstraints { (make) -> Void in
                    self.bottomConstraint =  make.bottom.equalTo(pictureView.snp_bottom).offset(TimelineCellMargin).constraint
                }
                
            }
            else
            {
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(textView.snp_bottom).constraint
                })
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI()
    {
        backgroundColor = UIColor.whiteColor()
        
        addSubview(iconView)
        addSubview(nameView)
        addSubview(mbrank)
        addSubview(verified_type_image)
        addSubview(timeView)
        addSubview(sourceView)
        addSubview(textView)
        addSubview(pictureView)
        //添加约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(TimelineCellMargin)
            make.top.equalTo(self.snp_top).offset(TimelineCellMargin)
            make.size.equalTo(CGSizeMake(TimelineCellIconWidth, TimelineCellIconWidth))
        }
        
        
        nameView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconView.snp_right).offset(TimelineCellMargin)
            make.top.equalTo(iconView.snp_top)
        }
        
        mbrank.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameView.snp_right).offset(TimelineCellMargin)
            make.top.equalTo(nameView.snp_top)
        }
        
        verified_type_image.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_right)
            make.centerY.equalTo(iconView.snp_bottom)
        }
        
        timeView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameView.snp_left)
            make.bottom.equalTo(iconView.snp_bottom)
        }
        
        sourceView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(timeView.snp_right).offset(TimelineCellMargin)
            make.bottom.equalTo(iconView.snp_bottom)
            make.height.equalTo(timeView.snp_height)
        }
        
        textView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconView.snp_left)
            make.top.equalTo(iconView.snp_bottom).offset(TimelineCellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(textView.snp_bottom).offset(TimelineCellMargin)
            make.left.equalTo(textView.snp_left)
//            make.size.equalTo(CGSizeMake(100, 100))
        }
        
        //为topView设置约束,根据pictureView的底部设置
        self.snp_makeConstraints { (make) -> Void in
          self.bottomConstraint =  make.bottom.equalTo(pictureView.snp_bottom).offset(TimelineCellMargin).constraint
        }

        sourceView.addTarget(self, action: "sourceBtnDidClicked", forControlEvents: .TouchUpInside)
            }
    
    // MARK : 懒加载上面的控件
    //头像
    lazy var iconView : UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //名字
    lazy var nameView = UILabel(title: "卡酷伊", color: UIColor.orangeColor(), fontSize: 13)
    //皇冠
    lazy var mbrank = UIImageView(image: UIImage(named: "common_icon_membership"))
    //大V(头像右下角)
    lazy var verified_type_image = UIImageView(image: UIImage(named: "avatar_vip"))
    //发布时间
    lazy var timeView = UILabel(title: "06:06", color: UIColor.orangeColor(), fontSize: 10)
    //发布来源
//    lazy var sourceView = UILabel(title: "斯..斯锅伊", color: UIColor.orangeColor(), fontSize: 10)
    lazy var sourceView = UIButton(title: "", color: themeColor, fontSize: 10)
    
    //内容
    lazy var textView = UILabel(title: "文能提笔控萝莉,武能床上定人妻文能提笔控萝莉,武能床上定人妻文能提笔控萝莉,武能床上定人妻", color: UIColor.blackColor(), fontSize: 15, margin: TimelineCellMargin)
    
    //配图
    lazy var pictureView : TimelineCellPictureView = TimelineCellPictureView()
}
